package com.hoperay.util;

import java.io.Serializable;
import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Country;
import com.liferay.portal.model.Phone;
import com.liferay.portal.model.Region;
import com.liferay.portal.model.User;
import com.liferay.portal.service.CountryServiceUtil;
import com.liferay.portal.service.RegionServiceUtil;
import com.slayer.model.AddressExt;
import com.slayer.model.City;
import com.slayer.model.impl.CityImpl;
import com.slayer.service.AddressExtLocalServiceUtil;
import com.slayer.service.CityLocalServiceUtil;

public class GeoLocation implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public GeoLocation(String data) {
		
		String[] tokens = data.split(StringPool.COMMA);

		setCountryCode(tokens[0]);
		setRegionCode(tokens[1]);
		setCity(tokens[2]);
		setLatitude(tokens[3]);
		setLongitude(tokens[4]);
	}
	
	public GeoLocation(User user){
		
		Address address = getUserAddress(user);

		if (Validator.isNotNull(address)) {
			this.city = address.getCity();
			
			Country country_ = address.getCountry();
			this.countryId = country_.getCountryId();
			this.countryCode = country_.getA2();
			this.country = country_.getName();
			this.idd = country_.getIdd();
			this.addressId =  address.getAddressId();
			
			Region region_ = address.getRegion();
			this.regionId = region_.getRegionId();
			this.regionCode = region_.getRegionCode();
			this.region = region_.getName();
			
			this.street1 = address.getStreet1();
			this.street2 = address.getStreet2();
			this.zip = address.getZip();
			
			this.latitude = null;
			this.longitude = null;
			
			try {
				AddressExt addressExt = AddressExtLocalServiceUtil.fetchAddressExt(address.getAddressId());
				this.cityId = addressExt.getCityId();
				} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				List<Phone> phones = user.getPhones();
				for (Phone phone: phones) {
					if (phone.isPrimary()) {
						this.primaryPhone = phone.getNumber();
						this.primaryPhoneId = phone.getPhoneId();
					} else {
						this.secondaryPhone = phone.getNumber();
						this.secondaryPhoneId = phone.getPhoneId();
					}
				}
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static Address getUserAddress(User user) {
		Address address = null;
		
		try {
			List<Address> userAddresses = user.getAddresses();
			for (Address userAddress: userAddresses) {
				if (userAddress.isPrimary()) {
					address = userAddress;
					break;
				}
			}
	
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return address;
	}
	
	public long getCountryId() {
		return countryId;
	}
	public void setCountryId(long countryId) {
		this.countryId = countryId;
	}
	public long getRegionId() {
		return regionId;
	}
	public void setRegionId(int regionId) {
		this.regionId = regionId;
	}
	public long getCityId() {
		return cityId;
	}
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}
	
	public String getCountryCode() {
		return countryCode;
	}
	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
		
		try {
			Country _country = CountryServiceUtil.getCountryByA2(countryCode);
			
			this.countryId = _country.getCountryId();
			this.country = _country.getName();
			this.idd = _country.getIdd();
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public String getRegionCode() {
		return regionCode;
	}
	public void setRegionCode(String regionCode) {
		this.regionCode = regionCode;
		
		try {
			List<Region> regions = RegionServiceUtil.getRegions(this.countryId);
			
			for (Region region: regions) {
				if (region.getRegionCode().equalsIgnoreCase(regionCode)) {
					this.regionId = region.getRegionId();
					this.region = region.getName();
					break;
				}
			}
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getCity() {
		return city;
	}
	
	public void setCity(String cityName) {
		this.city = cityName;
		
		City city = null;
		
		try {
			city = CityLocalServiceUtil.getCityByRegionId_CityName(this.regionId, cityName);
			
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		if (Validator.isNull(city) && this.regionId > 0l) {
			long cityId = 0l;
			try {
				cityId = CounterLocalServiceUtil.increment();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			city = new CityImpl();
			city.setCityId(cityId);
			city.setRegionId(this.regionId);
			city.setCityName(cityName);
			city.setStatus(true);
			
			try {
				city = CityLocalServiceUtil.addCity(city);
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		this.cityId = city.getCityId();
	}	
	
	public String getLatitude() {
		return latitude;
	}
	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}
	public String getLongitude() {
		return longitude;
	}
	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}
	
	public String getIdd() {
		return idd;
	}

	private long countryId;
	private long regionId;
	private long cityId;
	
	private String countryCode;
	private String regionCode;
	private String city;
	
	private String latitude;
	private String longitude;
	
	private String region;
	private String country;
	private String idd;
	
	private String street1;
	private String street2;
	private String primaryPhone;
	private String secondaryPhone;
	private String zip;
	
	private long addressId;
	private long primaryPhoneId;
	private long secondaryPhoneId;
	
	private boolean isPrimaryMobile = true;
	private boolean isSecondaryMobile = true;
	
	public boolean isPrimaryMobile() {
		return isPrimaryMobile;
	}

	public void setPrimaryMobile(boolean isPrimaryMobile) {
		this.isPrimaryMobile = isPrimaryMobile;
	}

	public boolean isSecondaryMobile() {
		return isSecondaryMobile;
	}

	public void setSecondaryMobile(boolean isSecondaryMobile) {
		this.isSecondaryMobile = isSecondaryMobile;
	}

	public long getAddressId() {
		return addressId;
	}

	public void setAddressId(long addressId) {
		this.addressId = addressId;
	}

	public long getPrimaryPhoneId() {
		return primaryPhoneId;
	}

	public void setPrimaryPhoneId(long primaryPhoneId) {
		this.primaryPhoneId = primaryPhoneId;
	}

	public long getSecondaryPhoneId() {
		return secondaryPhoneId;
	}

	public void setSecondaryPhoneId(long secondaryPhoneId) {
		this.secondaryPhoneId = secondaryPhoneId;
	}
	
	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getStreet1() {
		return street1;
	}

	public void setStreet1(String street1) {
		this.street1 = street1;
	}

	public String getStreet2() {
		return street2;
	}

	public void setStreet2(String street2) {
		this.street2 = street2;
	}

	public String getPrimaryPhone() {
		return primaryPhone;
	}

	public void setPrimaryPhone(String primaryPhone) {
		this.primaryPhone = primaryPhone;
	}

	public String getSecondaryPhone() {
		return secondaryPhone;
	}

	public void setSecondaryPhone(String secondaryPhone) {
		this.secondaryPhone = secondaryPhone;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setRegionId(long regionId) {
		this.regionId = regionId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public void setIdd(String idd) {
		this.idd = idd;
	}

	public String getFormattedText() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(this.city).append(", ");
		sb.append(this.region).append(", ");
		sb.append(this.country).append(".");
				
		return sb.toString();
	}

	@Override
	public String toString() {
		return "GeoLocation [countryId=" + countryId + ", regionId=" + regionId
				+ ", cityId=" + cityId + ", countryCode=" + countryCode
				+ ", regionCode=" + regionCode + ", city=" + city
				+ ", latitude=" + latitude + ", longitude=" + longitude
				+ ", region=" + region + ", country=" + country + ", idd="
				+ idd + ", street1=" + street1 + ", street2=" + street2
				+ ", primaryPhone=" + primaryPhone + ", secondaryPhone="
				+ secondaryPhone + ", zip=" + zip + ", addressId=" + addressId
				+ ", primaryPhoneId=" + primaryPhoneId + ", secondaryPhoneId="
				+ secondaryPhoneId + ", isPrimaryMobile=" + isPrimaryMobile
				+ ", isSecondaryMobile=" + isSecondaryMobile + "]";
	}
}